import MenuRole2 from '@/components/menuRole/menuRole2';
import { getGroupPerMenuList, updateGroupPerMenuList } from '@/services/permission';
import { PageContainer } from '@ant-design/pro-layout';
import { Button, Card, message } from 'antd';
import React, { useEffect, useState } from 'react';
import { getMenuList as queryMenu } from '@/services/menu';
const Index: React.FC = (props) => {
  //list
  const [dataList, setDataList] = useState([]);
  //menu
  const [menuList, setMenuList] = useState([]);

  useEffect(() => {
    getMenuList();
    getRoleList();
  }, []);

  const getMenuList = async () => {
    const res = await queryMenu();
    let menuOptions = [];
    if (res.code == '0') {
      if (!!res.data && res.data.length > 0) {
        res.data.forEach((element) => {
          menuOptions.push({
            label: element.name,
            value: element.id,
          });
        });
      }

      setMenuList(menuOptions);
    }
  };
  const getRoleList = async () => {
    const res = await getGroupPerMenuList();
    if (res.code == '0') {
      setDataList(res.data);
    }
  };

  const onSubmit = async () => {
    const params = {
      groupPerList: dataList,
    };
    console.log(params);
    const res = await updateGroupPerMenuList(params);
    if (res.code == '0') {
      message.success('保存成功！');
    } else {
      message.error('保存失败！');
    }
  };
  return (
    <PageContainer
      header={{
        title: false,
      }}
    >
      <Card title="部门全局权限设置">
        <div key={new Date().getTime()}>
          {dataList.map((item) => {
            return (
              <MenuRole2
                groupItem={item}
                key={item.groupId}
                menuList={menuList}
                dataList={dataList}
                setDataList={setDataList}
              />
            );
          })}
        </div>
        {dataList.length > 0 ? (
          <Button type="primary" style={{ marginLeft: '50%', marginTop: 20 }} onClick={onSubmit}>
            保存
          </Button>
        ) : null}
      </Card>
    </PageContainer>
  );
};

export default Index;
